package com.example.myapplication_2.util;

import android.os.RecoverySystem;
import android.util.Log;




import java.util.concurrent.TimeUnit;

import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * @author Liu Hui
 * @date 2018/9/9
 */

public class OkHttpUtil {

    private static OkHttpClient okHttpClient = new OkHttpClient.Builder()
            .connectTimeout(20000, TimeUnit.MILLISECONDS)
            .readTimeout(20000, TimeUnit.MILLISECONDS)
            .writeTimeout(20000, TimeUnit.MILLISECONDS).build();

//    public static void postFile(String url, final ProgressListener listener, Callback callback, File... files) {
//        MultipartBody.Builder builder = new MultipartBody.Builder();
//        builder.setType(MultipartBody.FORM);
//        builder.addFormDataPart("file", files[0].getName(), RequestBody.create(MediaType.parse("application/octet-stream"), files[0]));
//        MultipartBody multipartBody = builder.build();
//        Request request = new Request.Builder().url(url).post(new ProgressRequestBody(multipartBody, listener)).build();
//        okHttpClient.newCall(request).enqueue(callback);
//    }

//    public static void postFile(String url, final ProgressListener listener, Callback callback, String cosUrl) {
//        MultipartBody.Builder builder = new MultipartBody.Builder();
//        builder.setType(MultipartBody.FORM);
//        builder.addFormDataPart("imgpath", cosUrl, RequestBody.create(MediaType.parse("application/octet-stream"), cosUrl));
//        MultipartBody multipartBody = builder.build();
//        Request request = new Request.Builder().url(url).post(new ProgressRequestBody(multipartBody, listener)).build();
//        okHttpClient.newCall(request).enqueue(callback);
//    }


    public static void postFile(String url, final RecoverySystem.ProgressListener listener, Callback callback, String cosUrl) throws Exception{
//        OkHttpClient OkHttpClient = new OkHttpClient();
        //构建一个请求对象
        Request request = new Request.Builder().url(url+cosUrl).build();
        try{
            Long time1 = System.currentTimeMillis();

            Response Response = okHttpClient.newCall(request).execute();
            Log.i("---message*******",Response.body().string());
            //发送请求
            okHttpClient.newCall(request).enqueue(callback);

            Long time2 = System.currentTimeMillis();

            if((time2 - time1)>20000){
                throw new Exception();
            }
        }catch (Exception e){
//            e.printStackTrace();
            throw e;
        }


    }
}
